﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmAdduser : Form
    {
        public int UserId = 0;
        public int loginId = 0;

        public frmAdduser()
        {
            InitializeComponent();

            dgUser.AutoGenerateColumns = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateUser())
            {
                LDMSRepository repository = new LDMSRepository();
                Login login = new Login();
                login.CreatedBy = UserId;
                login.CreatedDate = DateTime.Now;
                login.DisplayName = txtDisplayName.Text.Trim();
                login.IsActive = chkIsActive.Checked;
                login.Password = txtPassword.Text.Trim();
                login.UserName = txtuserName.Text.Trim();
                login.LoginID = loginId;
                repository.AddUser(login);
                MessageBox.Show("User is saved successfully", Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            txtDisplayName.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtuserName.Text = string.Empty;
            txtConfirmPassword.Text = string.Empty;
            chkIsActive.Checked = false;
            loginId = 0;
        }

        private bool ValidateUser()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtuserName.Text))
            {
                MessageBox.Show(Resources.RequiredUserName, Resources.MessageCaption);
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtDisplayName.Text))
            {
                MessageBox.Show(Resources.RequiredDisplayName, Resources.MessageCaption);
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show(Resources.RequiredPassword, Resources.MessageCaption);
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtConfirmPassword.Text))
            {
                MessageBox.Show(Resources.RequiredConfirmPassword, Resources.MessageCaption);
                isValid = false;
            }

            if (!txtPassword.Text.ToLower().Equals(txtConfirmPassword.Text.ToLower()))
            {
                MessageBox.Show(Resources.MismatchedPassword, Resources.MessageCaption);
                isValid = false;
            }

            return isValid;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SearchUser()
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();

            if (!string.IsNullOrEmpty(txtSDisplayName.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.DISPLAYNAME, txtSDisplayName.Text));
            }

            if (!string.IsNullOrEmpty(txtSUserName.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.USERNAME, txtSUserName.Text));
            }

            //if (chkSearchIsActive.Checked)
            //{
            parameters.Add(new KeyValuePair<string, string>(Constants.ACTIVE, chkSearchIsActive.Checked.ToString()));
            // }
            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var contacts = repository.SearchUser(parameters);
                dgUser.DataSource = contacts;
            }
            else { MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption); }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchUser();
        }

        private void dgUser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgUser.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("Edit User"))
            {
                txtuserName.Text = Convert.ToString(dgUser.Rows[e.RowIndex].Cells["UserName"].Value);

                txtDisplayName.Text = Convert.ToString(dgUser.Rows[e.RowIndex].Cells["DisplayName"].Value);

                txtPassword.Text = Convert.ToString(dgUser.Rows[e.RowIndex].Cells["Password"].Value);

                txtConfirmPassword.Text = Convert.ToString(dgUser.Rows[e.RowIndex].Cells["Password"].Value);

                loginId = Convert.ToInt32(dgUser.Rows[e.RowIndex].Cells["ColLoginID"].Value);

                chkIsActive.Checked = Convert.ToBoolean(dgUser.Rows[e.RowIndex].Cells["IsActive"].Value);

                dgUser.DataSource = null;
            }
        }
    }
}
